﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;

using LuxDiner.Model.Accounts;
using LuxDiner.Logic.Accounts;
namespace LuxDiner.Web.Global.Membership
{
    public class MembershipProvider:System.Web.Security.MembershipProvider
    {
        public override string ApplicationName
        {
            get
            {
                return "LuxDiner";
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        private MembershipUser Convert(UserModel user)
        {
            if (user != null)
            {
                MembershipUser result = new MembershipUser(null, user.UserName, user.ID, user.Email, null, null, true, true, user.CreatedOn, user.LastLoginTime == null ? DateTime.MinValue : user.LastLoginTime.Value, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue);
                return result;
            }
            return null;
        }
        private MembershipUserCollection Convert(UserCollection users)
        {
            MembershipUserCollection result = new MembershipUserCollection();
            if (users != null && users.TotalRecordsCount > 0)
            {
                users.ForEach(item =>
                {
                    MembershipUser u = Convert(item);
                    if (u != null) result.Add(u);
                });
            }
            return result;
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            return UserLogic.ChangePassword(username, oldPassword, newPassword);
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordReset
        {
            get { return false; }
        }

        public override bool EnablePasswordRetrieval
        {
            get { return false; }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            var user = UserLogic.GetUser(username);
            return Convert(user);
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            try
            {
                var guid = (string)providerUserKey;
                var user = UserLogic.GetUser(guid);
                return Convert(user);
            }
            catch { return null; }
        }

        public override string GetUserNameByEmail(string email)
        {
            throw new NotImplementedException();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredPasswordLength
        {
            get { throw new NotImplementedException(); }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { throw new NotImplementedException(); }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

        public override bool ValidateUser(string username, string password)
        {
            throw new NotImplementedException();
        }
    }
}
